import 'package:built_collection/built_collection.dart';
import 'package:backend.core/core/domain/entities/api/web/api_web_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/web/web_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/web/web_subscription_duration.dart';
import 'package:backend.core/core/domain/entities/web/yookassa/yookassa_purchased_subscription_transaction.dart';
import 'package:backend.purchases/domain/helpers/api/web/yookassa/create_api_yookassa_purchased_subscription_transaction_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/get_web_purchased_subscription_transaction_expiration_time_map.dart';

abstract class CreateApiWebPurchasedSubscriptionTransactionsFromInternalOnes {
  BuiltList<ApiWebPurchasedSubscriptionTransaction> call({
    required BuiltList<WebPurchasedSubscriptionTransaction> transactions,
    required WebSubscriptionDuration subscriptionDuration,
    required bool test,
  });
}

class CreateApiWebPurchasedSubscriptionTransactionsFromInternalOnesImpl
    implements CreateApiWebPurchasedSubscriptionTransactionsFromInternalOnes {
  const CreateApiWebPurchasedSubscriptionTransactionsFromInternalOnesImpl({
    required CreateApiYookassaPurchasedSubscriptionTransactionFromInternalOne
        createApiYookassaPurchasedSubscriptionTransactionFromInternalOne,
    required GetWebPurchasedSubscriptionTransactionExpirationTimeMap
        getWebPurchasedSubscriptionTransactionExpirationTimeMap,
  })  : _createApiYookassaPurchasedSubscriptionTransactionFromInternalOne =
            createApiYookassaPurchasedSubscriptionTransactionFromInternalOne,
        _getWebPurchasedSubscriptionTransactionExpirationTimeMap =
            getWebPurchasedSubscriptionTransactionExpirationTimeMap;

  final CreateApiYookassaPurchasedSubscriptionTransactionFromInternalOne
      _createApiYookassaPurchasedSubscriptionTransactionFromInternalOne;
  final GetWebPurchasedSubscriptionTransactionExpirationTimeMap
      _getWebPurchasedSubscriptionTransactionExpirationTimeMap;

  @override
  BuiltList<ApiWebPurchasedSubscriptionTransaction> call({
    required BuiltList<WebPurchasedSubscriptionTransaction> transactions,
    required WebSubscriptionDuration subscriptionDuration,
    required bool test,
  }) {
    final transactionExpirationMap = _getWebPurchasedSubscriptionTransactionExpirationTimeMap(
      transactions: transactions,
      subscriptionDuration: subscriptionDuration,
      test: test,
    );

    final apiTransactions = <ApiWebPurchasedSubscriptionTransaction>[];

    for (final transaction in transactions) {
      late final ApiWebPurchasedSubscriptionTransaction apiTransaction;

      final expirationTime = transactionExpirationMap[transaction.id]!;

      if (transaction is YookassaPurchasedSubscriptionTransaction) {
        apiTransaction = _createApiYookassaPurchasedSubscriptionTransactionFromInternalOne(
          transaction: transaction,
          expirationTime: expirationTime,
        );
      }

      apiTransactions.add(apiTransaction);
    }

    return apiTransactions.toBuiltList();
  }
}
